package com.edu.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.user.domain.dto.DoctorUpdateDTO;
import com.edu.user.domain.dto.UserUpdateDTO;
import com.edu.user.domain.po.Doctor;
import com.edu.user.domain.po.User;
import com.edu.user.domain.vo.DoctorInfoVO;
import com.edu.user.domain.vo.DoctorVO;
import com.edu.user.mapper.DoctorMapper;
import com.edu.user.service.DoctorService;
import com.edu.user.utils.BeanUtils;
import com.edu.user.utils.JwtUtil;
import com.edu.user.utils.Result;
import com.edu.user.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.edu.user.utils.RedisConstants.LOGIN_USER_KEY;
import static com.edu.user.utils.RedisConstants.LOGIN_USER_TTL;

@Service
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements DoctorService {
    @Autowired
    UserServiceImpl userService;
    @Autowired
    private DoctorMapper doctorMapper;
    @Override
    public List<DoctorVO> getAll() {
        List<Doctor> list = list();
        List<DoctorVO>list1=new ArrayList<>();
        for (Doctor doctor : list) {
            DoctorVO doctorVO = BeanUtils.copyProperties(doctor, DoctorVO.class);
            User user = userService.getById(doctor.getUserId());
            doctorVO.setName(user.getName());
            doctorVO.setHeadUrl(user.getHeadUrl()); // 获取头像地址
            list1.add(doctorVO);
        }

        return list1;


    }

    @Override
    public DoctorVO getDetailById(Integer doctorId) {
        QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", doctorId);

        Doctor doctor = doctorMapper.selectOne(queryWrapper);
        DoctorVO doctorVO = BeanUtils.copyProperties(doctor, DoctorVO.class);
        User user = userService.getById(doctor.getUserId());
        doctorVO.setName(user.getName());
        doctorVO.setHeadUrl(user.getHeadUrl()); // 获取头像地址
        // 将查询到的数据封装为 Result 对象返回给前端
            return doctorVO;
    }
    @Override
    public List<DoctorVO> getDoctorsByDepartment(String department) {
        QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department", department);

        List<Doctor> doctors = doctorMapper.selectList(queryWrapper);
        List<DoctorVO> doctorVOS = new ArrayList<>();
        for (Doctor doctor : doctors) {
            DoctorVO doctorVO = BeanUtils.copyProperties(doctor, DoctorVO.class);
            User user = userService.getById(doctor.getUserId());
            doctorVO.setName(user.getName());
            doctorVO.setHeadUrl(user.getHeadUrl()); // 获取头像地址
            doctorVOS.add(doctorVO);
        }
        return doctorVOS;
    }

    @Override
    public List<DoctorVO> getDoctorsByName(String name) {
        QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>();
        List<Doctor> doctors = doctorMapper.selectList(queryWrapper);
        List<DoctorVO> doctorVOS = new ArrayList<>();
        for (Doctor doctor : doctors) {
            User user = userService.getById(doctor.getUserId());
            if (user.getName().contains(name)) {
                DoctorVO doctorVO = BeanUtils.copyProperties(doctor, DoctorVO.class);
                doctorVO.setName(user.getName());
                doctorVO.setHeadUrl(user.getHeadUrl());
                doctorVOS.add(doctorVO);
            }
        }
        return doctorVOS;
    }

    @Override
    public DoctorInfoVO getDoctorInfoById(Long userId) {
        DoctorVO detailById = getDetailById(Math.toIntExact(userId));
        DoctorInfoVO doctorInfoVO = BeanUtils.copyProperties(detailById, DoctorInfoVO.class);
        User user = userService.getById(userId);
        BeanUtils.copyProperties(user,doctorInfoVO);
        return doctorInfoVO;


    }

    @Override
    public Result<String> updateDoctorProfile(DoctorUpdateDTO updatedDoctor, Long doctorId) {
        QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", doctorId);

        Doctor doctor = doctorMapper.selectOne(queryWrapper);
        doctor.setBio(updatedDoctor.getBio());
        doctor.setSpecialty(updatedDoctor.getSpecialty());
        updateById(doctor);;
        String token = userService.updateUserName(doctorId, updatedDoctor.getName());
        if (token==null){
            return Result.error("找不到用户信息，更新用户名失败");
        }
        return Result.success(token);
    }


}
